package com.yihuier.yihuier.Service.MeetingManager;

import com.yihuier.yihuier.Entity.MeetingManager.DTO.MeetingApplyDTO;
import com.yihuier.yihuier.Entity.MeetingManager.MeetingApply;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingApplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MeetingApplyService {

    @Autowired
    private MeetingApplyMapper meetingApplyMapper;

    /**
     * 根据用户编号获取用户自己申请的会议
     * */
    public List<MeetingApply> getPersonalApplyMeetings(int user_id){
        return meetingApplyMapper.selectPersonalApplyMeetings(user_id);
    }

    /**
     * 获取用户申请会议的数量
     * @param user_id 用户ID
     * @return 申请会议数量
     */
    public int getNumOfMeetingApplyByUser(int user_id){
        return meetingApplyMapper.selectNumOfMeetingApplyByApplicant(user_id);
    }

    public int updateMeetingApply(MeetingApplyDTO meetingApply){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp = null;
        try {
            temp = sdf.parse(meetingApply.getMeeting_time());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String em_list = meetingApply.getEm_list();
        String hardware_applicant = meetingApply.getHardware_applicant();
        if (em_list.startsWith("[")){
            em_list = em_list.substring(1);
        }
        if (em_list.endsWith("]")){
            em_list = em_list.substring(0,em_list.length() - 1);
        }

        if (hardware_applicant.startsWith("[")){
            hardware_applicant = hardware_applicant.substring(1);
        }
        if (hardware_applicant.endsWith("]")){
            hardware_applicant = hardware_applicant.substring(0,hardware_applicant.length() - 1);
        }

        meetingApply.setEm_list(em_list);
        meetingApply.setHardware_applicant(hardware_applicant);


        return this.meetingApplyMapper.updateMeetingApply(
                meetingApply.getApp_meeting_id(),
                meetingApply.getApp_name(),
                meetingApply.getApp_description(),
                temp,
                meetingApply.getDuring_time(),
                meetingApply.getLocation(),
                meetingApply.getEm_number(),
                em_list,
                meetingApply.getStatus(),
                meetingApply.getApplicant(),
                hardware_applicant,
                meetingApply.getIsIntelligence_allocation(),
                meetingApply.getOther());
    }

}
